/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import liquibase.pro.packaged.E;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.mach.entity.EquipmentCategory;
import org.springblade.mach.vo.EquipmentCategoryVO;
import org.springblade.mach.mapper.EquipmentCategoryMapper;
import org.springblade.mach.service.IEquipmentCategoryService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 设备类别表 服务实现类
 *
 * @author zhp
 * @since 2020-12-28
 */
@Service
public class EquipmentCategoryServiceImpl extends BaseServiceImpl<EquipmentCategoryMapper, EquipmentCategory> implements IEquipmentCategoryService {

	@Override
	public IPage<EquipmentCategoryVO> selectEquipmentCategoryPage(IPage<EquipmentCategoryVO> page, EquipmentCategoryVO equipmentCategory) {
		return page.setRecords(baseMapper.selectEquipmentCategoryPage(page, equipmentCategory));
	}

	@Override
	public List<EquipmentCategoryVO> tree() {
		return ForestNodeMerger.merge(baseMapper.tree());
	}

	@Override
	public boolean removeEquipmentCategory(String ids) {
		Integer cnt = baseMapper.selectCount(Wrappers.<EquipmentCategory>query().lambda().in(EquipmentCategory::getParentId, Func.toLongList(ids)));
		if (cnt > 0) {
			throw new ServiceException("请先删除子节点!");
		}
		return removeByIds(Func.toLongList(ids));
	}

	@Override
	public List<EquipmentCategory> getParentNode() {
		return  baseMapper.selectList(Wrappers.<EquipmentCategory>query().lambda()
			.eq(EquipmentCategory::getParentId, 0));
	}

	@Override
	public List<EquipmentCategory> getChildNode(Long parentId) {
		return baseMapper.selectList(Wrappers.<EquipmentCategory>query().lambda()
			.eq(EquipmentCategory::getParentId,parentId));
	}

	@Override
	public String getEquipmentCodeById(Long id) {
		return baseMapper.getEquipmentCodeById(id);
	}

	/**
	 * @Author zhp
	 * @Description //TODO
	 * @Date 9:52 2021/11/19
	 * @param id
	 * @return org.springblade.mach.entity.EquipmentCategory
	 * 先从缓存中取,找不到在读数据库,并将结果放入缓存
	*/
	public EquipmentCategory getById(Long id) {
		EquipmentCategory equipmentCategory = (EquipmentCategory) CacheUtil.get("EquipmentCategory", "EquipmentCategory", id);
		if (equipmentCategory == null) {
			equipmentCategory = super.getById(id);
			CacheUtil.put("EquipmentCategory", "EquipmentCategory", id, equipmentCategory);
		}
		return equipmentCategory;
	}
}
